﻿using System.Collections.Generic;
using System.Text;
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using System.Xml;

namespace Ramparts
{
    public class AnimatedSprite
    {

        float timer;
        public bool loop;
        public bool playing;
        float interval = 1000;
        List<Rectangle> frames;
        int currentFrame;
        SceneManager sceneManager;
        Texture2D texture;

        // Create a GameObject with the given information
        // TODO CHANGE TO USE A TEXTURE SO WE'RE NOT RELOADING TEXTURES ALL THE TIME
        public AnimatedSprite(String textureName, SceneManager inSceneManager)
        {
            sceneManager = inSceneManager;
            texture = sceneManager.rampart.content.Load<Texture2D>(textureName);
            frames = new List<Rectangle>();
            currentFrame = 0;
            this.sceneManager = inSceneManager;
            playing = false;
        }

        public void Draw(Vector2 position)
        {
            sceneManager.rampart.spriteBatch.Draw(
                texture,
                position,
                frames[currentFrame],
                Color.White);
        }

        public void Update(GameTime gameTime)
        {

            if (!playing)
                return;
    
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            
            if (timer > interval)
            {
                currentFrame++;
                if (currentFrame > frames.Count - 1)
                {
                    if (!loop)
                        currentFrame = 0;
                    else
                    {
                        currentFrame = frames.Count - 1;
                        playing = false;
                    }
                }
                timer = 0f;
            }

        }

        public void addFrame(Rectangle rectangle)
        {

            frames.Add(rectangle);

        }


    }

}